In [1]:
%matplotlib inline

Simple-Calculations-Exercises

01.Square Area

The first task on this topic is to write a console program that introduces an integer a calculates the face of a square with side a. The task is trivially easy: you enter a number from the console, multiply it by yourself and print the result of the console.


In [2]:
a = float(input())

area = a * a

print(area)


5
25.0

02.Inches to Centimeters

Write a program that reads a console number (not necessarily a whole) and converts the number from inches in centimeters. For this purpose, it multiplies the inches by 2.54 (because 1 inch = 2.54 centimeters).


In [3]:
inches = float(input('inches = '))

centimeters = inches * 2.54

print('Centimeters = ',centimeters)


inches = 5
Centimeters =  12.7

03.Greeting by Name

Write a program that reads a person's name from the console and prints "Hello, (name)!", Where (name) is the previously entered name.


In [5]:
name = input()

print(f'Hello, {name}!')


Yani
Hello, Yani!

04.Concatenate Data

Write a Python program that reads from the console name, surname, age, and city and prints a message from type: "You are (firstName), (lastName), (age) years old person from (town);"


In [7]:
first_name = input()
last_name = input()
age = int(input())
town = input()

print(f"You are {first_name} {last_name}, a {age}-years old person from {town}.")


Yani
Lozanov
21
Plovdiv
You are Yani Lozanov, a 21-years old person from Plovdiv.

05. Trapeziod Area

Write a program that reads from the console three real numbers b1, b2 and h and calculates the trapeze face with bases b1 and b2 and height h. The trapezoidal face formula is (b1 + b2) * h / 2.


In [8]:
b1 =  float(input())
b2 = float(input())
h = float(input())

area = ((b1 + b2) * h) / 2

print(area)


5
5
5
25.0

06.Circle Area and Perimeter

Write a program that reads from the console number r and calculates and prints the face and perimeter of a circle / circle with radius r.


In [1]:
import math

r = float(input())

area = math.pi * r * r
perimeter = 2 * math.pi * r

print(f"Area = {area}")
print(f"Perimeter = {perimeter}")


3
Area = 28.274333882308138
Perimeter = 18.84955592153876

07.2D Rectangle Area

A rectangle is given with the coordinates of two of its opposite angles (x1, y1) - (x2, y2). Calculate its area and perimeter. The input is read from the console. The numbers x1, y1, x2 and y2 are given in one order. The output is output to the console and must contain two rows with one number each - face and perimeter.


In [3]:
x1 = float(input())
y1 = float(input())
x2 = float(input())
y2 = float(input())


side_x = abs(x1 - x2)
side_y = abs(y1 - y2)

area = side_x * side_y
perimeter = side_x * 2 + side_y * 2

print(area)
print(perimeter)


60
20
10
50
1500.0
160.0

08.Triangle Area

Write a program that reads from the console side and triangle height and calculates its face. Use the face to triangle formula: area = a * h / 2. Round the result to 2 decimal places using float ("{0: .2f}".


In [4]:
side = float(input())
height = float(input())

area = (side * height) / 2

print("Triangle area = ",float("{0:.2f}".format(area)))


7.75
8.45
Triangle area =  32.74

09.Celsius to Fahrenheit

Write a program that reads degrees on the Celsius scale (°C) and converts them to degrees Fahrenheit (°F). Look for an appropriate formula on the Internet to make the calculations. Round the score to 2 decimal places.


In [5]:
celsius = float(input())

fahrenheit = celsius * 9/5 + 32

print(float("{0:.2f}".format(fahrenheit)))


35
95.0

10.Radians to Degrees

Write a program that reads an angle in radians (rad) and converts it into degrees (deg). Look for an appropriate formula online. The number π in Python programs is available through math.pi. Round the result to the nearest integer using round ().


In [6]:
import  math

radians = float(input())

degrees = round((radians * 180) / math.pi)

print(degrees)


3.1416
180

11.USD to BGN

Write down a program to convert US Dollars (USD) into Bulgarian Leva (BGN). Round the score to 2 digits after the decimal point. Use a fixed exchange rate between USD and BGN: 1 USD = 1.79549 BGN.


In [7]:
dollars = float(input())

leva = dollars * 1.79549

print(float("{0:.2f}".format(leva)),"BGN")


35
62.84 BGN

12.Currency Converter

Write a program for converting money from one currency to another. The following currencies need to be maintained: BGN, USD, EUR, GBP. Use the following fixed exchange rates:

BGN = 1.79549 USD / 1.95583 EUR / 2.53405 GBP

The input is a conversion amount + Input Currency + Output Currency. The output is one number - the converted amount on the above courses, rounded to 2 digits after the decimal point.


In [8]:
amount = float(input())
first_currency = input()
second_currency = input()

result = 0

if first_currency == "BGN":

    if second_currency == "BGN":
       result = amount * 1

    elif second_currency == "USD":
        result = amount / 1.79549

    elif second_currency == "EUR":
        result = amount / 1.95583

    elif second_currency == "GBP":
        result = amount / 2.53405

elif first_currency == "USD":

    if second_currency == "BGN":
        result = amount * 1.79549

    elif second_currency == "USD":
        result = amount * 1

    elif second_currency == "EUR":
        result = (amount * 1.79549) / 1.95583

    elif second_currency == "GBP":
        result = (amount * 1.79549) / 2.53405

elif first_currency == "EUR":

    if second_currency == "BGN":
        result = amount * 1.95583

    elif second_currency == "USD":
        result = (amount * 1.95583 ) / 1.79549

    elif second_currency == "EUR":
        result = amount * 1

    elif second_currency == "GBP":
        result = (amount * 1.95583 ) / 2.53405


elif first_currency == "GBP":

    if second_currency == "BGN":
        result = amount * 2.53405

    elif second_currency == "USD":
        result = (amount * 2.53405) / 1.79549

    elif second_currency == "EUR":
        result = (amount * 2.53405) / 1.95583

    elif second_currency == "GBP":
        result = amount * 1

print(float("{0:.2f}".format(result)),second_currency)


20
USD
BGN
35.91 BGN